class Solution {
public:
    int maxLength(vector<int>& arr) {
        int n = arr.size();
        int begin = 0, end = 0;
        unordered_map<int, int> count;
        int ret = 0;
        while (end < n)
        {
            count[arr[end]]++;
            while (count[arr[end]] == 2)
            {
                count[arr[begin]]--;
                begin++;
            }
            ret = max(ret, end - begin + 1);

            end++;
        }

        return ret;
    }
};